package com.iknow.xmpp.service;

import android.os.RemoteException;
import com.iknow.IKnow;
import com.iknow.User;
import com.iknow.db.IKnowDataBase;
import com.iknow.util.Loger;
import com.iknow.xmpp.extention.SessionIQ;
import com.iknow.xmpp.extention.UserInfomationIQ;
import com.iknow.xmpp.service.aidl.IKnowUser.Stub;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.RosterPacket.Item;
import org.jivesoftware.smack.packet.RosterPacket.ItemType;
import org.jivesoftware.smack.packet.XMPPError;

public class MyIKnowUser extends IKnowUser.Stub
{
  private final String TAG = "MyIKnowUser";
  private XMPPConnection mConnection;

  public MyIKnowUser(XMPPConnection paramXMPPConnection)
  {
    this.mConnection = paramXMPPConnection;
  }

  private boolean sendIQPacketSync(IQ paramIQ)
  {
    int i;
    IQ localIQ;
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(paramIQ.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(localAndFilter);
    try
    {
      this.mConnection.sendPacket(paramIQ);
      localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
      localPacketCollector.cancel();
      if (localIQ == null)
      {
        Loger.e("MyIKnowUser", "No response from server.");
        i = 0;
        return i;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      while (true)
      {
        while (true)
        {
          Loger.e("MyIKnowUser", localIllegalStateException.getMessage());
          i = 0;
        }
        if (localIQ.getType() != IQ.Type.ERROR)
          break;
        Loger.e("MyIKnowUser", "sendIQPacketSync error!!");
        i = 0;
      }
      i = 1;
    }
  }

  public boolean bindSession(String paramString)
    throws RemoteException
  {
    SessionIQ localSessionIQ = new SessionIQ();
    localSessionIQ.setSesssionID(paramString);
    localSessionIQ.setType(IQ.Type.SET);
    return sendIQPacketSync(localSessionIQ);
  }

  public boolean cancelFollow(String paramString1, String paramString2)
  {
    int j;
    IQ localIQ;
    int i = 1;
    RosterPacket localRosterPacket = new RosterPacket();
    localRosterPacket.setType(IQ.Type.SET);
    RosterPacket.Item localItem = new RosterPacket.Item(paramString1, paramString2);
    localItem.setItemType(RosterPacket.ItemType.none);
    localRosterPacket.addRosterItem(localItem);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(new PacketIDFilter(localRosterPacket.getPacketID()));
    try
    {
      this.mConnection.sendPacket(localRosterPacket);
      localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
      localPacketCollector.cancel();
      if (localIQ == null)
      {
        Loger.e("MyIKnowUser", "No response from the server.");
        i = 0;
        j = i;
        return j;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      do
        while (true)
        {
          Loger.e("MyIKnowUser", localIllegalStateException.getMessage());
          j = 0;
        }
      while (localIQ.getType() != IQ.Type.ERROR);
      Loger.e("MyIKnowUser", localIQ.getError().getMessage());
      i = 0;
    }
  }

  public boolean chagePassword(String paramString)
    throws RemoteException
  {
    int i;
    AccountManager localAccountManager = new AccountManager(this.mConnection);
    try
    {
      localAccountManager.changePassword(paramString);
      i = 1;
      return i;
    }
    catch (XMPPException localXMPPException)
    {
      Loger.e("MyIKnowUser", localXMPPException.getMessage());
      localXMPPException.printStackTrace();
      i = 0;
    }
  }

  public boolean followFriend(String paramString1, String paramString2)
    throws RemoteException
  {
    return true;
  }

  public List<Friend> getNearByFriends(double paramDouble1, double paramDouble2, double paramDouble3, int paramInt1, int paramInt2)
    throws RemoteException
  {
    ArrayList localArrayList2;
    IQ localIQ;
    UserInfomationIQ localUserInfomationIQ1 = new UserInfomationIQ();
    localUserInfomationIQ1.addAttributes("surround", "latitude", String.valueOf(paramDouble2));
    localUserInfomationIQ1.addAttributes("surround", "longitude", String.valueOf(paramDouble1));
    localUserInfomationIQ1.addAttributes("surround", "limit", String.valueOf(paramDouble3));
    localUserInfomationIQ1.put("offset", String.valueOf(paramInt1));
    localUserInfomationIQ1.put("length", String.valueOf(paramInt2));
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localUserInfomationIQ1.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(localAndFilter);
    ArrayList localArrayList1 = new ArrayList();
    try
    {
      this.mConnection.sendPacket(localUserInfomationIQ1);
      localIQ = (IQ)localPacketCollector.nextResult(13000L);
      localPacketCollector.cancel();
      if (localIQ == null)
      {
        Loger.e("MyIKnowUser", "No response from server.");
        localArrayList2 = null;
        return localArrayList2;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      UserInfomationIQ localUserInfomationIQ2;
      int j;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            Loger.e("MyIKnowUser", localIllegalStateException.getMessage());
            localArrayList2 = null;
          }
          if (localIQ.getType() != IQ.Type.ERROR)
            break;
          Loger.e("MyIKnowUser", localIQ.getError().getMessage());
          localArrayList2 = null;
        }
        if (localIQ instanceof UserInfomationIQ)
        {
          localUserInfomationIQ2 = (UserInfomationIQ)localIQ;
          int i = localUserInfomationIQ2.getFriendListCount();
          j = 0;
          if (j < i)
            break;
          localUserInfomationIQ2.clearFriendList();
        }
        localArrayList2 = localArrayList1;
      }
      localArrayList1.add(localUserInfomationIQ2.getFriendByIndex(j));
      ++j;
    }
  }

  public Friend getUserInfo(String paramString)
    throws RemoteException
  {
    Object localObject;
    IQ localIQ;
    UserInfomationIQ localUserInfomationIQ1 = new UserInfomationIQ();
    localUserInfomationIQ1.put("user_id", "<item>" + paramString + "</item>");
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localUserInfomationIQ1.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(localAndFilter);
    try
    {
      this.mConnection.sendPacket(localUserInfomationIQ1);
      localIQ = (IQ)localPacketCollector.nextResult(10000L);
      localPacketCollector.cancel();
      if (localIQ == null)
      {
        Loger.e("MyIKnowUser", "No response from server.");
        localObject = null;
        label247: return localObject;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      while (true)
      {
        UserInfomationIQ localUserInfomationIQ2;
        while (true)
        {
          while (true)
          {
            while (true)
            {
              Loger.e("MyIKnowUser", localIllegalStateException.getMessage());
              localObject = null;
            }
            if (localIQ.getType() != IQ.Type.ERROR)
              break;
            if ((localIQ.getError() != null) && (localIQ.getError().getMessage() != null))
              Loger.e("MyIKnowUser", localIQ.getError().getMessage());
            localObject = null;
          }
          if (!(localIQ instanceof UserInfomationIQ))
            break label247;
          localUserInfomationIQ2 = (UserInfomationIQ)localIQ;
          if (localUserInfomationIQ2.getFriendListCount() != 0)
            break;
          localObject = null;
        }
        Friend localFriend = localUserInfomationIQ2.getFriendByIndex(0);
        localUserInfomationIQ2.clearFriendList();
        localObject = localFriend;
      }
      localObject = null;
    }
  }

  public List<Friend> queryFriendsByNick(String paramString, int paramInt1, int paramInt2)
  {
    ArrayList localArrayList1;
    IQ localIQ;
    ArrayList localArrayList2;
    UserInfomationIQ localUserInfomationIQ1 = new UserInfomationIQ();
    localUserInfomationIQ1.put("nickName", paramString);
    localUserInfomationIQ1.put("offset", String.valueOf(paramInt1));
    localUserInfomationIQ1.put("length", String.valueOf(paramInt2));
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localUserInfomationIQ1.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(localAndFilter);
    try
    {
      this.mConnection.sendPacket(localUserInfomationIQ1);
      localIQ = (IQ)localPacketCollector.nextResult(13000L);
      localPacketCollector.cancel();
      localArrayList2 = new ArrayList();
      if (localIQ == null)
      {
        Loger.e("MyIKnowUser", "No response from server.");
        localArrayList1 = null;
        return localArrayList1;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      UserInfomationIQ localUserInfomationIQ2;
      int j;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            Loger.e("MyIKnowUser", localIllegalStateException.getMessage());
            localArrayList1 = null;
          }
          if (localIQ.getType() != IQ.Type.ERROR)
            break;
          if ((localIQ.getError() != null) && (localIQ.getError().getMessage() != null))
            Loger.e("MyIKnowUser", localIQ.getError().getMessage());
          localArrayList1 = null;
        }
        if (localIQ instanceof UserInfomationIQ)
        {
          localUserInfomationIQ2 = (UserInfomationIQ)localIQ;
          int i = localUserInfomationIQ2.getFriendListCount();
          j = 0;
          if (j < i)
            break;
          localUserInfomationIQ2.clearFriendList();
        }
        localArrayList1 = localArrayList2;
      }
      localArrayList2.add(localUserInfomationIQ2.getFriendByIndex(j));
      ++j;
    }
  }

  public boolean queryOnlineUsers()
    throws RemoteException
  {
    int i;
    IQ localIQ;
    UserInfomationIQ localUserInfomationIQ1 = new UserInfomationIQ();
    localUserInfomationIQ1.put("online", "1");
    localUserInfomationIQ1.put("offset ", "0");
    localUserInfomationIQ1.put("length ", "20");
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new PacketIDFilter(localUserInfomationIQ1.getPacketID());
    arrayOfPacketFilter[1] = new PacketTypeFilter(IQ.class);
    AndFilter localAndFilter = new AndFilter(arrayOfPacketFilter);
    PacketCollector localPacketCollector = this.mConnection.createPacketCollector(localAndFilter);
    try
    {
      this.mConnection.sendPacket(localUserInfomationIQ1);
      localIQ = (IQ)localPacketCollector.nextResult(30000L);
      localPacketCollector.cancel();
      if (localIQ == null)
      {
        Loger.e("MyIKnowUser", "No response from server.");
        i = 0;
        return i;
      }
    }
    catch (IllegalStateException localIllegalStateException)
    {
      UserInfomationIQ localUserInfomationIQ2;
      int k;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            Loger.e("MyIKnowUser", localIllegalStateException.getMessage());
            i = 0;
          }
          if (localIQ.getType() != IQ.Type.ERROR)
            break;
          if ((localIQ.getError() != null) && (localIQ.getError().getMessage() != null))
            Loger.e("MyIKnowUser", localIQ.getError().getMessage());
          i = 0;
        }
        if (localIQ instanceof UserInfomationIQ)
        {
          localUserInfomationIQ2 = (UserInfomationIQ)localIQ;
          int j = localUserInfomationIQ2.getFriendListCount();
          if (j > 0)
            IKnow.mIKnowDataBase.deleteNearByFriend();
          k = 0;
          if (k < j)
            break;
          localUserInfomationIQ2.clearFriendList();
        }
        i = 1;
      }
      Friend localFriend = localUserInfomationIQ2.getFriendByIndex(k);
      IKnow.mIKnowDataBase.addNearByFriend(localFriend);
      ++k;
    }
  }

  public boolean subscribeTag(String paramString)
    throws RemoteException
  {
    UserInfomationIQ localUserInfomationIQ = new UserInfomationIQ();
    localUserInfomationIQ.setType(IQ.Type.SET);
    localUserInfomationIQ.put("subscribeFlag", paramString);
    localUserInfomationIQ.put("email", IKnow.mIKnowDataBase.getUser().getEmail());
    return sendIQPacketSync(localUserInfomationIQ);
  }

  public boolean updateUserEmail(String paramString)
    throws RemoteException
  {
    UserInfomationIQ localUserInfomationIQ = new UserInfomationIQ();
    localUserInfomationIQ.setType(IQ.Type.SET);
    localUserInfomationIQ.put("email", paramString);
    return sendIQPacketSync(localUserInfomationIQ);
  }

  public boolean updateUserInfo(String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
    throws RemoteException
  {
    UserInfomationIQ localUserInfomationIQ = new UserInfomationIQ();
    localUserInfomationIQ.setType(IQ.Type.SET);
    if (paramString2 != null)
      localUserInfomationIQ.put("nickName", paramString2);
    if (paramString3 != null)
      localUserInfomationIQ.put("userFlag", paramString3);
    if (paramString4 != null)
      localUserInfomationIQ.put("signature", paramString4);
    if (paramString5 != null)
      localUserInfomationIQ.put("gender", paramString5);
    if (paramString1 != null)
      localUserInfomationIQ.put("photoUrl", paramString1);
    localUserInfomationIQ.put("email", IKnow.mIKnowDataBase.getUser().getEmail());
    return sendIQPacketSync(localUserInfomationIQ);
  }

  public boolean updateUserLocation(double paramDouble1, double paramDouble2)
    throws RemoteException
  {
    UserInfomationIQ localUserInfomationIQ = new UserInfomationIQ();
    localUserInfomationIQ.setType(IQ.Type.SET);
    localUserInfomationIQ.put("latitude", String.valueOf(paramDouble2));
    localUserInfomationIQ.put("longitude", String.valueOf(paramDouble1));
    localUserInfomationIQ.put("email", IKnow.mIKnowDataBase.getUser().getEmail());
    return sendIQPacketSync(localUserInfomationIQ);
  }

  public boolean updateUserPhoto(String paramString)
    throws RemoteException
  {
    return false;
  }

  public boolean updateUsersubscribeTags(String paramString)
    throws RemoteException
  {
    return false;
  }
}